﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Reflection;

namespace MVC.ApiExplorer
{
	public class ActionExploreController : Controller
	{
		[ChildActionOnly]
		public ActionResult Explore(Type controllerType, string RouteName = "")
		{
			var methods = from m in controllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
						  where (m.GetCustomAttributes(typeof(NonActionAttribute), true).Length == 0)
								&&
								(m.GetCustomAttributes(typeof(NonApiAttribute), true).Length == 0)
								&&
								(
									m.ReturnType.IsSubclassOf(typeof(ActionResult))
									||
									m.ReturnType.Equals(typeof(ActionResult))
								)

						  select m;

			ViewData["routeName"] = RouteName;
			ViewData["controllerName"] = controllerType.Name.Substring(0, controllerType.Name.LastIndexOf("Controller"));
			return View(new ApiExploreView(methods.ToList()));
		}

	}
}
